<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>字符串子串计数器</title>
    <style>
        body {
            font-family: 'Arial', sans-serif;
            background-color: #f4f7fa;
            color: #333;
            margin: 0;
            padding: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
        }

        .container {
            background-color: #ffffff;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            padding: 20px;
            border-radius: 8px;
            width: 400px;
            text-align: center;
        }

        h1 {
            color: #4CAF50;
            margin-bottom: 20px;
        }

        label {
            font-size: 16px;
            display: block;
            margin-bottom: 8px;
            text-align: left;
        }

        input {
            width: 100%;
            padding: 10px;
            margin: 10px 0;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 16px;
            box-sizing: border-box;
        }

        button {
            background-color: #4CAF50;
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 4px;
            font-size: 16px;
            cursor: pointer;
            transition: background-color 0.3s;
        }

        button:hover {
            background-color: #45a049;
        }

        .result {
            margin-top: 20px;
            font-size: 18px;
            font-weight: bold;
            color: #333;
        }
    </style>
</head>

<body>

    <div class="container">
        <h1>字符串子串计数器</h1>
        <label for="inputString">请输入字符串：</label>
        <input type="text" id="inputString" placeholder="例如：hello world, hello everyone" />

        <label for="searchValue">请输入要搜索的子串：</label>
        <input type="text" id="searchValue" placeholder="例如：hello" />

        <button onclick="calculateCount()">计算出现次数</button>

        <div class="result" id="result"></div>
    </div>

    <script>
        const countSubstrings = (str, searchValue) => {
            let count = 0,
                i = 0;
            while (true) {
                const r = str.indexOf(searchValue, i);
                if (r !== -1) {
                    [count, i] = [count + 1, r + 1];
                } else {
                    return count;
                }
            }
        };

        function calculateCount() {
            const str = document.getElementById('inputString').value;
            const searchValue = document.getElementById('searchValue').value;
            const resultDiv = document.getElementById('result');

            if (str === '' || searchValue === '') {
                resultDiv.textContent = '请输入完整的字符串和子串!';
                resultDiv.style.color = 'red';
                return;
            }

            const count = countSubstrings(str, searchValue);
            resultDiv.textContent = `子串 "${searchValue}" 出现的次数: ${count}`;
            resultDiv.style.color = '#333';
        }
    </script>

</body>

</html>